<?php 

namespace app\index\controller;
use think\Controller;
class Login extends Controller{

	//登录
	public function login(){
		return $this->fetch();
	}

	//注册
	public function regist(){
		foreach(model('Village')->where(['pid'=>'1'])->select() as $val){
			$data_area[] = $val->toArray();
		}
		// dump($data_area);
		$this->assign('data_area',$data_area);
		return $this->fetch();
	}

	//找回密码
	public function forget(){
		$this->assign('step',1);
		if(request()->ispost()){
			if(input('post.tel') != null){
				$this->assign('step',2);
				session('forget_tel',input('post.tel'));
			}elseif(input('post.code') != null){
				// if(session('verify') == input('post.code')){
					$this->assign('step',3);
				// }		
			}elseif(input('post.pwd') != null){
				$where = [
					'tel'=>session('forget_tel'),
				];
				$change = [
					'pwd'=>md5(input('post.pwd')),
				];
				model('Vip')->where($where)->update($change);
				$this->assign('step',4);
			}
		}
		return $this->fetch();
	}

	//处理注册
	public function do_regist(){
		$vld = $this->validate(input('post.'),'Vip.regist');
		if($vld !== true){
			$this->error($vld);
		}
		$_POST['pwd'] = md5($_POST['pwd']);
		$res = model('Vip')->allowfield(true)->save($_POST);
		if($res > 0){
			$this->success($res);
		}else{
			$this->error('注册失败');
		}
	}

	//处理登录
	public function do_login(){
		$vld = $this->validate(input('post.'),'Vip.login');
		if($vld !== true){
			$this->error($vld);
		}
		$where1 = ['tel'=>input('post.user'),'pwd'=>md5(input('post.pwd'))];
		$where2 = ['nickname'=>input('post.user'),'pwd'=>md5(input('post.pwd'))];
		$count1 = model('Vip')->where($where1)->count();
		$count2 = model('Vip')->where($where2)->count();
		$count = $count1+$count2;
		if($count > 0){
			if($count1 > 0){
				$nickname = model('Vip')->where($where1)->find()->nickname;
				session('nickname',$nickname);
			}else{
				session('nickname',input('post.user'));
			}
			//是否记住密码
			if(input('post.remb') == 1){
				cookie('user',input('post.user'),3600*24*30);
				cookie('pwd',input('post.pwd'),3600*24*30);
			}else{
				cookie('user',null);
				cookie('pwd',null);
			}
			$this->success('成功');
		}else{
			$this->error('失败');
		}
	}

	//注销
	public function logout(){
		session('nickname',null);
		$this->redirect('index/index/index');
	}

	//check
	public function check(){
		$where = [
			'tel'=>$_GET['tel']
		];
		$count = model('Vip')->where($where)->count();
		if($count >0){
			echo 'false';
		}else{
			echo 'true';
		}
	}

	//check2
	public function check2(){
		$where = [
			'tel'=>$_GET['tel']
		];
		$count = model('Vip')->where($where)->count();
		if($count >0){
			echo 'true';
		}else{
			echo 'false';
		}
	}

	//check3
	// public function check3(){
	// 	;
	// 	if($_GET['code'] == session('verify')){
	// 		echo 'true';
	// 	}else{
	// 		echo 'false';
	// 	}
	// }

	//check4
	public function check4(){
		$where = [
			'nickname'=>$_GET['nickname']
		];
		$count = model('Vip')->where($where)->count();
		if($count >0){
			echo 'false';
		}else{
			echo 'true';
		}
	}

}